const formatTime = timestamp => {
  let date = new Date(timestamp);
  console.log( date.toLocaleString())
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
    // return year
  return `${[year, month, day].map(formatNumber).join('/')} `
}


const formatTime2 = timestamp => {
    let date = new Date(timestamp);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  }
  

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 加密
const Encrypt = require('./jsencrypt');
import {
  PulicKey
} from './config'
let cryptFirst = new Encrypt.JSEncrypt();
cryptFirst.setPublicKey(PulicKey);
const passwordJsencrypt = (str) => {
  return cryptFirst.encrypt(str)
}


/**
 * 检测当前的小程序
 * 是否是最新版本，是否需要下载、更新
 */

const checkUpdateVersion = () => {
  //判断微信版本是否 兼容小程序更新机制API的使用
  if (wx.canIUse('getUpdateManager')) {
    const updateManager = wx.getUpdateManager();
    //检测版本更新
    updateManager.onCheckForUpdate(function (res) {
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: '温馨提示',
            content: '检测到新版本，是否重启小程序？',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
        updateManager.onUpdateFailed(function () {
          // 新版本下载失败
          wx.showModal({
            title: '已有新版本',
            content: '请您删除小程序，重新搜索进入',
          })
        })
      }
    })
  } else {
    wx.showModal({
      title: '溫馨提示',
      content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
    })
  }
}

const  algorithm=()=>{
	let abc=['a','b','c','d','e','f','g','h','i','g','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
	let [max,min]=[Math.floor(Math.random()*(10-7+1)+1),Math.floor(Math.random()*(17-10+1)+17)];
	abc = abc.sort(() => 0.4-Math.random()).slice(max,min).slice(0,8).join("");
	let a = new Date().getTime() + abc;
	return a;
}


module.exports = {
  formatTime,
  passwordJsencrypt,
  checkUpdateVersion,
  algorithm,
  formatTime2
}